import { BreezeClass } from "./BreezeClass";
import { IElement } from "../interface/elementType";
import { PropAndBindingClass } from "./PropAndBindingClass";

export class ElementClass extends BreezeClass<IElement> {

  private children?: ElementClass[];
  private bindings?: PropAndBindingClass[];
  private props?: PropAndBindingClass[];

  constructor(props: IElement) {
    super({
      props: PropAndBindingClass,
      bindings: PropAndBindingClass,
      children: ElementClass
    }, props);
  }

  getField() {
    return this.field;
  }

  getChildren() {
    return this.children;
  }

  getProps() {
    return this.props;
  }

  getBindings() {
    return this.bindings;
  }

  getProp(field: string) {
    return (this.props || []).find((prop) => prop.field = field);
  }

  getBinding(field: string) {
    return (this.bindings || []).find((binding) => binding.field = field);
  }

  getElementByUUID(uuid: string) {
    let element: ElementClass | null | undefined = null;
    if (this.uuid === uuid) {
      element = this;
    }
    if (this.children) {
      element = this.children.find(child => child.uuid === uuid);
      !element && this.children.some(child => {
        return !!(element = child.getElementByUUID(uuid));
      })
    }
    return element;
  }

  deleteProp(name: string) {
    this.props = this.props?.filter(prop => prop.field !== name);
  }
  deleteBinding(name: string) {
    this.bindings = this.bindings?.filter(prop => prop.field !== name);
  }
}